package dao

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"teacher-helper/common"
)

var (
	DB *gorm.DB
	db_err error
)

func InitDB() (err error) {
	db_type, _ := common.GetSystemDBType()
	if db_type == "mysql" {
		return InitMysql()
	}
	return
}

func Close() {
	err := DB.Close()
	if err != nil {
		panic(err)
	}
}

func InitMysql() error {
	types, local, _, err := common.GetMysqlConfig()
	if err != nil {
		panic(err)
	}
	DB, db_err = gorm.Open(types, local)
	if db_err != nil {
		panic(db_err)
	}
	return DB.DB().Ping()
}